package com.coinland.study.widgt;

import android.animation.PropertyValuesHolder;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Shader;
import android.os.Build;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.support.v7.widget.AppCompatImageView;
import android.util.AttributeSet;

/**
 * 类描述: 从一个圆角矩形平滑过渡到圆形View
 * (来源：https://blog.csdn.net/qq_23900685/article/details/90172126)
 * 创建人:  shenZ
 * 创建时间: 2020/4/22
 * 联系方式: 252079249@qq.com
 * 修改时间: 2020/4/22
 */
public class Rectangle2CircleView extends AppCompatImageView {

    private static final String PROPERTY_RADIUS = "property_radius";
    private static final String PROPERTY_ROTATE = "property_scale";
    private RectF rectF = new RectF();
    private int radius = 0;
    private Paint backgroundPaint;
    private ValueAnimator animator;

    private Matrix mMatrix;
    private BitmapShader mBitmapShader;

    public Rectangle2CircleView(Context context) {
        this(context, null);
    }

    public Rectangle2CircleView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public Rectangle2CircleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        //初始化图片变换处理器
        mMatrix = new Matrix();

        backgroundPaint = new Paint();
        backgroundPaint.setAntiAlias(true);//抗锯齿,没有消除锯齿的话，图片变换会很难看的，因为有些像素点会失真
        backgroundPaint.setDither(true); //dither抖动的使用
        backgroundPaint.setStrokeWidth(10);
        backgroundPaint.setStyle(Paint.Style.FILL_AND_STROKE);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        int viewWidth = getWidth()/2 ;
        int viewHeight = getHeight()/2;
        int leftTopX = viewWidth-viewHeight;
        int leftTopY = 0;
        int rightBotX = viewWidth+viewHeight;
        int rightBotY = viewHeight+viewHeight;

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            canvas.drawRoundRect(leftTopX, leftTopY, rightBotX, rightBotY, radius, radius, backgroundPaint);
        } else {
            rectF.set(leftTopX, leftTopY, rightBotX, rightBotY);
            canvas.drawRoundRect(rectF, radius, radius, backgroundPaint);
        }

    }

    /**
     * 使用BitmapShader画圆图形
     */
    private void setBitmapShader(Bitmap bitmap) {
        //将bitmap放进图像着色器，后面两个模式是x，y轴的缩放模式，CLAMP表示拉伸
        mBitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
        //初始化图片与view之间伸缩比例，因为比例一般非整数，所以用float，免得精度丢失
        float scale = 1.0f;
        //将图片的宽度高度的最小者作为图片的边长，用来和view来计算伸缩比例
        int bitmapSize = Math.min(bitmap.getHeight(), bitmap.getWidth());

        //计算缩放比例，view的大小和图片的大小比例
        scale = getMeasuredWidth() * 1.0f / bitmapSize;
        //利用这个图像变换处理器，设置伸缩比例，长宽以相同比例伸缩
        mMatrix.setScale(scale, scale);
        //给那个图像着色器设置变换矩阵，绘制时就根据view的size，设置图片的size
        //使图片的较小的一边缩放到view的大小一致，这样就可以避免图片过小导致CLAMP拉伸模式或过大导致显示不全
        mBitmapShader.setLocalMatrix(mMatrix);
        //为画笔套上一个Shader的笔套
        backgroundPaint.setShader(mBitmapShader);
    }


    @Override
    public void setImageBitmap(Bitmap bm) {
        super.setImageBitmap(bm);
    }

    /**
     * 开启动画
     */
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public void startAnimation() {
        // 旋转属性
        PropertyValuesHolder propertyRadius = PropertyValuesHolder.ofInt(PROPERTY_RADIUS, 0,75);
        animator = new ValueAnimator();
        animator.setValues(propertyRadius);
        animator.setDuration(5000);
        animator.addUpdateListener(animation -> {
            radius = (int) animation.getAnimatedValue(PROPERTY_RADIUS);
            invalidate();
        });
        animator.start();
    }

}

